/**
 * a.tabljump 打开一个新的标签页面
 * a.modaljump 打开一个新的模态框
 * a.nojump 禁用a标签跳转
 * data-width:定义模态框宽度
 * data-height:定义高度
 * data-callback:定义回掉函数
 * data-saveType:定义保存类型
 */

var si = (function ($, si) {
    $(document).on("click", "a", function () {
        var src = $(this).attr("href");
        if (src == "#") {
            return false;
        }
        return true;
    })
    $(document).on("click", "a.tabjump", function () {
        var src = $(this).attr("href");
        if (src == "#" || src == undefined) {
            return false;
        }
        var name = $(this).text();
        try {
            window.parent.si.openTabWindow(src, name, src);
        } catch (err) {
            console.log(err);
            si.openTabWindow(src, name, src);
        } finally {
            return false;
        }
    });
    $(document).on("click", "a.nojump", function () {
        var action = $(this).attr("href");
         if(action==undefined||action=="#")
         {
             return false;
         }
         var callBack=$(this).attr("data-callback");
       if($(this).hasClass("confirm"))
       {
           layer.confirm('您确定要这么操作么？', {
               btn: ['确定','取消'] //按钮
           }, function(index,l){
               layer.msg('您确定了操作!');
                si.ajax(action,{},callBack);
           }, function(){
               layer.msg('您取消了操作!');
           });
       }else{
           si.ajax(action,{},callBack);
       }
       return false;
    });
    $(document).on("click", "a.modaljump", function () {
        var framedata_id = $(window.frameElement).attr("name");
        var par = {};
           par.src = $(this).attr("href");
        if ( par.src == "#" ||  par.src == undefined) {
            return false;
        }
        par.name = $(this).text();
        par.width=$(this).attr("data-width");
        par.height=$(this).attr("data-height");
        par.callback=$(this).attr("data-callback");
        par.saveType=$(this).attr("data-saveType");
        if(par.width==undefined)
        {
            par.width="860";
        }
        if(par.height==undefined)
        {
            par.height="480";
        }
        if(par.saveType==undefined)
        {
            par.saveType="saveAjax";
        }
        try {
            window.parent.si.openModel(par, framedata_id);
        } catch (err) {
            console.log(err);
            si.openModel(par, framedata_id);
        } finally {
            return false;
        }

    });

    si.saveAjax = function (elm, callBack) {
        var form = $(elm).find("form");
        var erromsg = si.validate(form);
        if (erromsg != undefined) {
            if(erromsg=="")
            {
                erromsg="提交失败，请检查信息！";
            }
            layer.alert('<img src="/admin/img/error.png" width="30" height="30"/> '+erromsg, {
                skin: 'layui-layer-molv'
                , closeBtn: 0
                , time: 3000
                , shade: 0
                , title: '错误信息'
                , anim: 0 //动画类型
                , zIndex: layer.zIndex
            });

        } else {
            var action = $(form).attr("action");
            if (action == undefined || action == "") {
                alert("提交数据时action没有定义");
                return ;
            }
            $.ajax({
                beforeSend: function () {
                    var index = layer.load(2, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });
                }, complete: function () {
                    layer.closeAll("loading");
                },
                cache: true,
                type: "POST",
                url: action,
                data: $(form).serializeJson(),// 你的formid
                async: false,
                error: function (request) {
                    layer.closeAll("loading");
                    layer.alert('<img src="/admin/img/error.png" width="30" height="30"/>  提交失败，请检查网络信息！', {
                        skin: 'layui-layer-molv'
                        , closeBtn: 0
                        , title: '错误信息'
                        , anim: 0 //动画类型
                        , zIndex: layer.zIndex
                    });
                },
                success: function (msg) {
                    callBack(msg)
                }
            });


        }



    };
    si.openModel = function (par, frame_data_id)
    {

        layer.open({
            type: 2,
            content: par.src,
            title:par.name,
            scrollbar:false,
            shade: false,
            area: [par.width+"px", par.height+"px"],
            offset: '40px',
            fix: false, //不固定
            maxmin: true,
            zIndex: layer.zIndex, //
            success: function(layero, index){
                layer.setTop(layero); //重点2

            }
            ,btn: ['保存','取消'],
            yes: function(index, layero){
                var body = layer.getChildFrame('body', index);
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                si.saveAjax(body, saveCallback);
                function saveCallback(msg) {
                     if (!msg.state) {
                        var errmsg = "提交失败，请稍后重试!";
                        if (msg.errors.length > 0) {
                            errmsg = msg.errors[0].errMsg;
                        } else {
                            errmsg = msg.msg;
                        }
                        layer.alert('<img src="/admin/img/error.png" width="30" height="30"/>&nbsp;&nbsp;' + errmsg, {
                             skin: 'layui-layer-molv'
                            , closeBtn: 0
                            , title: '错误信息'
                            ,anim: 0 //动画类型
                            ,zIndex: layer.zIndex,
                            success: function(layero){
                                layer.setTop(layero); //重点2
                            }
                        });
                        return;
                    }
                    layer.alert('<img src="/admin/img/success.png" width="30" height="30"/>&nbsp;&nbsp;' + msg.msg, {
                        skin: 'layui-layer-molv'
                        , closeBtn: 0
                        , time: 1000
                        , anim: 0 //动画类型
                        , zIndex: layer.zIndex
                        , success: function(layero, index){
                            layer.setTop(layero); //重点2
                        }
                    });
                    layer.close(index);
                    if (par.callback == undefined) {
                        try {
                            document.getElementsByName(frame_data_id)[0].contentWindow.frameCallBack(msg);
                        } catch (err) {
                            //
                        } finally {
                            return false;
                        }
                    } else {
                        var str = "document.getElementsByName(frame_data_id)[0].contentWindow." + par.callback + "(msg.data)";
                      // console.log(str);
                        //console.log(msg);
                        eval(str);
                    }
                }
            }
        });
    }
    si.validate = function (elm) {
        var error = $(elm).find("input.error");
        if (error.length > 0) {
            var a = $(error).next("label.error").eq(0).text();
           var label= $(error[0]).parent().parent().find("label.control-label").text();
            return label+a;
        }
    }
    si.ajax=function (action,data,callBack) {
        $.ajax({
            beforeSend: function () {
                var index = layer.load(2, {
                    shade: [0.1, '#fff'] //0.1透明度的白色背景
                });
            }, complete: function () {
                layer.closeAll("loading");
            },
            cache: true,
            type: "POST",
            url: action,
            data: data,// 你的formid
            async: false,
            error: function (request) {
                layer.closeAll("loading");
                layer.alert('<img src="/admin/img/error.png" width="30" height="30"/>  提交失败，请检查网络信息！', {
                    skin: 'layui-layer-molv'
                    , closeBtn: 0
                    , title: '错误信息'
                    , anim: 0 //动画类型
                    , zIndex: layer.zIndex
                });
            }, success: function (msg) {
                if(callBack==undefined||callBack==null)
                {
                  return ;
                }
                callBack+="(msg.data);";
                eval(callBack);
            }
        });
    }
    return si;
}($, window.si = {}));
